package com.oracle.Application.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.oracle.Application.config.JwtConfig;
import com.oracle.application.vo.R;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Data
@ConfigurationProperties("auth")
@Component
public class LoginFilter extends ZuulFilter {
    private String[] checkedUrl;
    @Autowired
    private JwtConfig jwtConfig;
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String requestURI = request.getRequestURI();
        for (int i = 0; i < checkedUrl.length; i++) {
            if (requestURI.startsWith(checkedUrl[i])){
                return true;
            }
        }
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("需要进行token验证");
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        HttpServletResponse response = context.getResponse();
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        String token = request.getHeader("token");
        if (token==null || "".equals(token)){
            //需要中断请求
            R r = new R(401, "请去登录页面login", null);
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
            return null;
        }
        try {
            this.jwtConfig.verifyToken(token);
        } catch (ExpiredJwtException e) {
            R r = new R(402, "令牌异常", null);
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (UnsupportedJwtException e) {
            R r = new R(402, "令牌异常", null);
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (MalformedJwtException e) {
            R r = new R(402, "令牌异常", null);
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (SignatureException e) {
            R r = new R(402, "令牌异常", null);
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (IllegalArgumentException e) {
            R r = new R(402, "令牌异常", null);
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        }
        return null;
    }
}
